/*==============================================================================
FILE NAME: 02_data_preparation.do
PURPOSE: To create all of the analysis data files used in figure and table construction.
CREATED: 10 July 2025
==============================================================================*/

/* Set directory if working independently through code
if c(username)=="" { //insert username
	global rootdir "" // insert root path
    global raw_data "$roodir/processed_data"
    global code "$rootdir/code"
	global processed_data "$rootdir/processed_data"  // Define global paths for replication package
} 
*/

*** MAIN FIGURES ***

//Run file to create survey data
do "$code/data_prep_code/Create_Survey_Data.do"

//Run files sequentially to create Air_Panel.dta
do "$code/data_prep_code/Convert_xlsx_to_dta.do"
do "$code/data_prep_code/Create_area_complaints.do"
do "$code/data_prep_code/Create_industry_complaints.do"
do "$code/data_prep_code/Create_county_complaints.do"
do "$code/data_prep_code/Create_region_complaints.do"
do "$code/data_prep_code/Create_NOE_cat.do"
do "$code/data_prep_code/Process_Facility_Characteristics.do"
do "$code/data_prep_code/Process_Incidents.do"
do "$code/data_prep_code/Process_Investigations.do"
do "$code/data_prep_code/Process_Enforcements.do"
do "$code/data_prep_code/Process_TitleV.do"
do "$code/data_prep_code/Process_NSR.do"
do "$code/data_prep_code/Process_Notice_of_Violations.do"
do "$code/data_prep_code/Explore_violation_severity.do" //where IN_vio_cat.dta is created; it is used in both Create_Air_Panel.do and Create_Air_Panel_Onsite_Included.do
do "$code/data_prep_code/Explore_Investigations.do" //where investigations_with_noe_nov.dta is created; it is used in Create_Air_Panel_Onsite_Included.do
do "$code/data_prep_code/Create_TitleV_Panel.do"
do "$code/data_prep_code/Create_Air_Panel.do"
do "$code/data_prep_code/Air_panel_with_referred.do"

//Run file below to create processed zipcode data
// First, generate and import mean PM 2.5 data; this is an R file. Must specify Rsript path
//NOTE: This file uses rgdal, which is a deprecated package. Downoad R 4.1.3 for this script to work!
shell Rscript "C:/Program Files/R/R-Latest/bin/Rscript.exe" "$code/data_prep_code/Create_Texas_PM25_Data.R"
do "$code/data_prep_code/Process_Zipcode_Census_Demographics.do"

//Run files to create Panel_inv_types_onsite_included_final.dta for Figures 4, 5, and 6
do "$code/data_prep_code/Process_Air_Investigations_Onsite_Flag.do"
do "$code/data_prep_code/Create_Air_Panel_Onsite_Included.do"

*** APPENDIX ***

//Run file to create Air_Panel_week.dta
//.dta files below are used in the appendix. If not reproducing appendix, lines can be commented out
do "$code/data_prep_code/Create_Air_Panel_Week.do"

//Run file to create Emissions_events_clean.dta
do "$code/appendix_code/appendix_data_prep_code/Process_Emissions_Events.do"
